package ar.com.ford.it.fullEconomics.persistence.dao;

import java.io.FileNotFoundException;
import java.util.Collection;
import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import oracle.toplink.queryframework.ReadAllQuery;
import oracle.toplink.sessions.Session;
import oracle.toplink.tools.sessionmanagement.SessionManager;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp01Part;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp02Commodity;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesr01Request;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesr02Requestitm;
import ar.com.ford.it.fullEconomics.persistence.dto.Teess01Supplier;
import ar.com.ford.it.fullEconomics.struct.RequestFindStruct;
import ar.com.ford.it.webengine.persistence.PersistenceUtil;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.LogFactory;
import com.ford.it.persistence.PersistenceException;
import com.ford.it.persistence.PersistenceManager;
import com.ford.it.persistence.Transaction;

public class RequestItemDao extends Dao {
    private static final String CLASS_NAME = CommodityDao.class.getName();
    private static final ILogger log = LogFactory.getInstance().getLogger(
            CLASS_NAME);

    public void createRequestItem(Teesr02Requestitm item)
            throws PersistenceException {

        String METHOD_NAME = "createRequestItem";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = null;

        tx = PersistenceUtil.getTransaction();
        tx.registerInsert(item);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    public Teesr02Requestitm getRequestItemForUpdate(Teesr01Request request,
                                                     Integer code,
                                                     Teesg01Plant plant)
            throws PersistenceException {

        String METHOD_NAME = "getRequestItemForUpdate";
        log.entering(CLASS_NAME, METHOD_NAME);

        Teesr02Requestitm result = null;
        Teesr02Requestitm pk = new Teesr02Requestitm();

        pk.setTeesr01Request(request);
        pk.setEesr02ReqitmNumberK(code);
        pk.setEesr02PlantCodeC(plant);

        result = (Teesr02Requestitm) super.getObjForUpdate(pk);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return result;
    }

    public void deleteRequestItem(Teesr02Requestitm item)
            throws PersistenceException {

        String METHOD_NAME = "deleteRequestItem";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = PersistenceUtil.getTransaction();
        if (tx != null) {
            tx.registerDelete(item);
        }

        log.exiting(CLASS_NAME, METHOD_NAME);
    }
    
    public Collection<Teesr02Requestitm> findRequestItems(RequestFindStruct findStruct, Teess01Supplier supplier, Teesp02Commodity commodity, Teesp01Part part) throws PersistenceException{
        String METHOD_NAME = "findRequestItems";
        log.entering(CLASS_NAME, METHOD_NAME);
        Collection<Teesr02Requestitm> result = null;
        PersistenceManager pm = null;

        Session session = null;

        try {
            session = SessionManager.getManager().getSession(
                    this.getSessionName());
        } catch (FileNotFoundException e) {
            throw new PersistenceException(e);
        }
        ExpressionBuilder builder = new ExpressionBuilder();
        ReadAllQuery query = new ReadAllQuery(Teesr02Requestitm.class);

        Expression exp = null;
        
        
        
        exp = builder.get("eesr02PlantCodeC").equal(findStruct.getPlant());
        exp = exp.and(builder.get("teesr01Request").get("teess01Supplier").equal(supplier));
        if(commodity != null)
            exp = exp.and(builder.get("teesp02Commodity").equal(commodity));
        if(part != null)
            exp = exp.and(builder.get("teesp04Supply").get("teesp01Part").equal(part));
        if(findStruct.getDateField().equals("R")){
            exp = exp.and(builder.get("teesr01Request").get("eesr01RequestIssueDateY").between(findStruct.getDateFrom(), findStruct.getDateTo()));
        }else if(findStruct.getDateField().equals("C")){
            exp = exp.and(builder.get("teesr01Request").get("eesr01RequestClosureS").between(findStruct.getDateFrom(), findStruct.getDateTo()));
        }
        if(findStruct.getUserField().equals("CR")){
            exp = exp.and(builder.get("teesr01Request").get("eesr01CreateUserC").equal(findStruct.getUserCode()));
        }else if(findStruct.getUserField().equals("CI")){
            exp = exp.and(builder.get("teesr01Request").get("eesr01RequestClosureUserC").equal(findStruct.getUserCode()));
        }
        if(findStruct.getStatus() != null && !findStruct.getStatus().equals("-1")){
            exp = exp.and(builder.get("teesr01Request").get("eesr01RequestStatusCodeC").equal(findStruct.getStatus()));
        }
        query.setSelectionCriteria(exp);
        result = (Collection<Teesr02Requestitm>) session.executeQuery(query);
        
        
        log.exiting(CLASS_NAME, METHOD_NAME);
        return result;
    }
}
